/**
 * author:Administrator
 * date:2016/3/29
 * desc:TODO
 */
'use strict';

let request = require('../common/appUtil.js').request;
let FormatOutput = require('../common/FormatOutput.js');
let FormatError = require('../common/FormatError.js');

let hostOfPlatform = 'http://localhost:1882';

let service = {};
module.exports = service;

/**
 * 获取地址
 */
service.location = function*(qs) {
  let path = '/api/v1/location';
  let opt = {
    url: hostOfPlatform + path,
    method: 'GET',
    qs: qs
  };
  return yield request(opt);
};

service.createLocation = function*(name, pId) {
  let path = '/api/v1/location';
  let opt = {
    url: hostOfPlatform + path,
    method: 'POST',
    headers: {'Content-type': 'application/json'},
    body: JSON.stringify({name: name, pid: pId})
  };
  return yield request(opt);
};


/**
 * 获取category
 */
service.category = function*(qs) {
  let path = '/api/v1/category';
  let opt = {
    url: hostOfPlatform + path,
    method: 'GET',
    qs: qs
  };
  return yield request(opt);
};

service.createCategory = function*(name, pId) {
  let path = '/api/v1/category';
  let opt = {
    url: hostOfPlatform + path,
    method: 'POST',
    headers: {'Content-type': 'application/json'},
    body: JSON.stringify({name: name, pid: pId})
  };
  return yield request(opt);
};

/**
 * user相关
 */
service.userRegister = function*(name, pwd) {
  let path = '/api/v1/user/register';
  let opt = {
    url: hostOfPlatform + path,
    method: 'POST',
    headers: {'Content-type': 'application/json'},
    body: JSON.stringify({name: name, pwd: pwd})
  };
  return yield request(opt);
};

/**
 * user相关
 */
service.userLogin = function*(name, pwd) {
  let path = '/api/v1/user/login';
  let opt = {
    url: hostOfPlatform + path,
    method: 'POST',
    headers: {'Content-type': 'application/json'},
    body: JSON.stringify({name: name, pwd: pwd})
  };
  return yield request(opt);
};

/**
 * 获取 user对应的role
 * @param uId
 */
service.allUserRole = function*(uId) {
  let path = '/api/v1/role';
  let opt = {
    qs: {uid: uId},
    url: hostOfPlatform + path,
    method: 'GET'
  };
  return yield request(opt);
};

service.createCustomer = function *(obj) {
  let path = '/api/v1/role/customer';
  let opt = {
    url: hostOfPlatform + path,
    method: 'POST',
    headers: {'Content-type': 'application/json'},
    body: JSON.stringify({name: obj.name, uid: obj.uId})
  };
  return yield request(opt);
};
service.createVendor = function *(obj) {
  let path = '/api/v1/role/vendor';
  let opt = {
    url: hostOfPlatform + path,
    method: 'POST',
    headers: {'Content-type': 'application/json'},
    body: JSON.stringify({name: obj.name, uid: obj.uId})
  };
  return yield request(opt);
};
service.createManager = function *(obj) {
  let path = '/api/v1/role/manager';
  let opt = {
    url: hostOfPlatform + path,
    method: 'POST',
    headers: {'Content-type': 'application/json'},
    body: JSON.stringify({name: obj.name, uid: obj.uId})
  };
  return yield request(opt);
};
service.createKeeper = function *(obj) {
  let path = '/api/v1/role/shopkeeper';
  let opt = {
    url: hostOfPlatform + path,
    method: 'POST',
    headers: {'Content-type': 'application/json'},
    body: JSON.stringify({name: obj.name, uid: obj.uId, vendorid: obj.vendorId})
  };
  return yield request(opt);
};